<?php
/**
* @name Maiji
* @author pd87@qq.com
* @copyright m-j.cc
* @version v1.0
*/
namespace MAIJI;

class Action
{
	private $smartys;
	
	/* 
		传入视图目录
	*/
    public function _sys_ini_run($url)
    {
		$this -> smartys = new \Smarty();
		// 默认就好了，true 每次请求强制刷新模板 false 不会强制刷新
		$this -> smartys -> force_compile = false;
		// 是否开启调试模式
		$this -> smartys -> debugging = C('basic.DEBUG');
		// 是否开启缓存
		$this -> smartys -> caching = C('smarty.caching');
		// 开启缓存的时间
		$this -> smartys -> cache_lifetime = C('smarty.cache_lifetime');
		//修改左右边界符号
		$this -> smartys -> left_delimiter = C('smarty.left_delimiter');
		$this -> smartys -> right_delimiter = C('smarty.right_delimiter');
		//设置模板路径
		$this -> smartys -> template_dir = APP_PATH . $url[0].'/view/';
		//设置模板缓存路径
		$this -> smartys -> compile_dir = APP_CACHE_PATH .$url[0].'_'.C('smarty.cachecata');
		//缓存目录
		$this -> smartys -> cache_dir = APP_CACHE_PATH;
		//配置目录
		$this -> smartys -> config_dir = APP_PATH.$url[0].'/';
		// 调试模板
		$this -> smartys -> debug_tpl = APP_INI_PATH.'tpl/debug.tpl';
    }
	
	/* 
	 * 前置方法
	*/
	public function _sys_front_run(){
		$_ls = get_class_methods($this);
		if(in_array('_construct',$_ls)){
			$this -> _construct();
		}
	}
	
	/* 
	 * 后置方法
	*/
	public function _sys_after_run(){
		if(C('basic.DEBUG')){
			$this -> setDelimiter('<{','}>');
			$_DEBUG = isset($GLOBALS['_DEBUG']) ? $GLOBALS['_DEBUG'] : null;
			$_DEBUG['qingqiu'] = date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' '.$_SERVER['SERVER_NAME'];
			$_DEBUG['time'] = round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'],6);
			$_DEBUG['get_included_files'] = get_included_files();
			$_DEBUG['memory_get_usage'] = round(memory_get_usage() / 1024,2) . 'KB';
			$_DEBUG['memory_get_peak_usage'] = round(memory_get_peak_usage() / 1024,2) . 'KB';
			$this->assign('_DEBUG',$_DEBUG);
			die($this -> fetch(APP_INI_PATH.'tpl/debug'));
		}
	}

	/**
     * 添加插件目录
     */
    public function addPluginsDir($dir){
        $this -> smartys -> addPluginsDir($dir);
    }
    
    /**
     * 重置smartyDelimiter
     */
    public function setDelimiter($left,$right){
         $this -> smartys -> left_delimiter = $left;
        $this -> smartys -> right_delimiter = $right;
    }
    
    /**
     * 设置smarty模板位
     */
    public function setTemplateDir($dir){
        $this -> smartys -> template_dir = $dir;
    }

    /**
     * smarty 设置模板变量
     */
    public function assign($name, $value) {
        $this -> smartys -> assign($name, $value);
    }

    /**
     * 获取模板解析后的内容
     */
    public function fetch($tplName=null) {
		global $_URL;
		if($tplName === null){
			return $this -> smartys -> fetch($_URL[1].C('basic.TPL_NAME'));
		}else{
			if(substr($tplName,-10) == '/tpl/debug' || substr($tplName,-11) == 'spathc_jump'){
				return $this -> smartys -> fetch($tplName.'.tpl');
			}else{
				return $this -> smartys -> fetch($tplName.C('basic.TPL_NAME'));
			}
		}
    }

    /**
     * 显示模板解析后的内容
     */
    public function display($tplName=null) {
		global $_URL;
		if($tplName === null){
			return $this -> smartys -> display($_URL[1].C('basic.TPL_NAME'));
		}else{
			if(substr($tplName,-10) == '/tpl/debug' || substr($tplName,-11) == 'spathc_jump'){
				return $this -> smartys -> display($tplName.'.tpl');
			}else{
				return $this -> smartys -> display($tplName.C('basic.TPL_NAME'));
			}
		}
    }
}